package com.chenyuhan.myview.testViewCanvas;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.chenyuhan.myview.R;

/**
 * Created by Administrator on 2017/7/5 0005.
 */

public class CanvasImageView extends View {
    private final String TAG = CanvasImageView.class.getSimpleName();
    private static final int WIDTH_NUMBER = 9;// 横向分割成的网格数量
    private static final int HEIGHT_NUMBER = 9;// 纵向分割成的网格数量
    private static final int COUNT = (WIDTH_NUMBER + 1) * (HEIGHT_NUMBER + 1);// 横纵向网格交织产生的点数量

    private Bitmap mBitmap;//位图资源

    private float[] matrixOriganal = new float[COUNT * 2];// 基准点坐标数组
    private float[] matrixMoved = new float[COUNT * 2];// 变换后点坐标数组
    private Paint origPaint;// 基准点绘制Paint

    public CanvasImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // 实例画笔并设置颜色
        origPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        origPaint.setColor(0x660000FF);
        //获取位图资源
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg);

        /*
         * 生成各个交点坐标
         */
        int index = 0;
        float multiple = mBitmap.getWidth();
        for (int y = 0; y <= HEIGHT_NUMBER; y++) {
            float fy = mBitmap.getHeight() * y / HEIGHT_NUMBER;
            for (int x = 0; x <= WIDTH_NUMBER; x++) {
                float fx = mBitmap.getWidth() * x / WIDTH_NUMBER;
//                float fx = mBitmap.getWidth() * x / WIDTH_NUMBER + ((HEIGHT_NUMBER - y) * 1.0F / HEIGHT_NUMBER * multiple);
                setXY(matrixOriganal,fx, fy, index);
                setXY(matrixMoved,fx, fy, index);
                index += 1;
            }
        }
    }

    public CanvasImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 将计算后的交点坐标存入数组
     *
     * @param fx    x坐标
     * @param fy    y坐标
     * @param index 标识值
     */
    private void setXY(float[] array, float fx, float fy, int index) {
        array[index * 2 + 0] = fx;
        array[index * 2 + 1] = fy;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 绘制网格位图
        canvas.drawBitmapMesh(mBitmap, WIDTH_NUMBER, HEIGHT_NUMBER, matrixMoved, 0, null, 0, null);
        drawGuide(canvas);
    }

    /**
     * 绘制点
     *
     * @param canvas 画布
     */
    private void drawGuide(Canvas canvas) {
        for (int i = 0; i < COUNT * 2; i += 2) {
            float x = matrixOriganal[i + 0];
            float y = matrixOriganal[i + 1];
            canvas.drawCircle(x, y, 4, origPaint);
        }
    }

    private float clickX, clickY;// 触摸屏幕时手指的xy坐标

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        clickX = event.getX();
        clickY = event.getY();
        smudge();
        invalidate();
        return true;
    }

    /**
     * 计算变换数组坐标
     */
    private void smudge() {
        for (int i = 0; i < COUNT * 2; i += 2) {

            float xOriginal = matrixOriganal[i + 0];
            float yOriginal = matrixOriganal[i + 1];

            float dist_click_to_origin_x = clickX - xOriginal;
            float dist_click_to_origin_y = clickY - yOriginal;

            float kv_kat = dist_click_to_origin_x * dist_click_to_origin_x + dist_click_to_origin_y * dist_click_to_origin_y;

            float pull = (float) (1000000 / kv_kat / Math.sqrt(kv_kat));

            if (pull >= 1) {
                matrixMoved[i + 0] = clickX;
                matrixMoved[i + 1] = clickY;
            } else {
                matrixMoved[i + 0] = xOriginal + dist_click_to_origin_x * pull;
                matrixMoved[i + 1] = yOriginal + dist_click_to_origin_y * pull;
            }
        }
    }
}
